/**
 * respons js file;
 * company:junqitecch
 * author:xuzuiotao
 * handling response information
 */


module.exports.toResponse=function(obj){
//   
//	var obj = JSON.parse(data);
//	this.command=obj.op;
//	
//	delete module.exports.ver;
//
//	//delete module.exports.command;
	deleteEmptyProperty(obj);
	var string = JSON.stringify(obj);//temp.stringify();
	return string;
};


module.exports.seq=0;
module.exports.ver=0;
module.exports.errorCode=0;
module.exports.message=null;
module.exports.data=null;


//删除空属性，避免null值出现在json中
function deleteEmptyProperty(object){
    for (var i in object) {
        var value = object[i];
        // sodino.com
        // console.log('typeof object[' + i + ']', (typeof value));
        if (typeof value === 'object') {
            if (Array.isArray(value)) {
                if (value.length === 0) {
                    delete object[i];
//                    console.log('delete Array', i);
                    continue;
                }
            }
            deleteEmptyProperty(value);
            if (isEmpty(value)) {
//                console.log('isEmpty true', i, value);
                delete object[i];
//                console.log('delete a empty object');
            }
        } else {
            if (value === '' || value === null || typeof(value) === undefined) {
                delete object[i];
//                console.log('delete ', i);
            } else {
//                console.log('check ', i, value);
            }
        }
    }
}

//判断 为空
function isEmpty(object) {
    for (var name in object) {
        return false;
    }
    return true;
}


class JQResponse {
    
	static makeResponse( data){
		
			let obj ={};
			obj.seq=0;
			obj.ver='3.0';
			obj.errorCode=0;
			obj.message='ok';
			
			global.JQNormal.deleteEmptyProperty(data);
			obj.data = JSON.stringify(data);
			return JSON.stringify(obj)
			
		
	}
	static makeErrorResponse( code,message){
		
			let obj ={};
			obj.seq=0;
			obj.ver='3.0';
			obj.errorCode=code;
			obj.message=message;
			
			return JSON.stringify(obj)
			
		
	}
	// static parserRequest(data){
	// 	try{
	// 		let obj = new JSON.parse(data);
	// 		return obj;
	// 	}catch(e){
	// 		console.log(e.message);
	// 	}
	// 	return null;
	// }
   
}
module.exports = JQResponse;


